package com.xiaobai.spike.web.client;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaobai.spike.entity.OauthClientDetails;
import com.xiaobai.spike.model.request.GetListByUserRequest;
import com.xiaobai.spike.model.request.OauthClientDetailAddRequest;
import com.xiaobai.spike.sdk.lang.ApiResponse;
import com.xiaobai.spike.service.client.OauthClientDetailsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/clientdetails")
public class ClientDetailsController {

    @Resource
    private OauthClientDetailsService oauthClientDetailsService;

    /**
     * 按道理 RequestHeader 应该在bff层 实现，现在只是懒了，直接写在服务上了
     * 服务按理说没有权限验证
     * @param userId
     * @param getListByUserRequest
     * @return
     */
    @PostMapping("/getListByUser")
    public ApiResponse<IPage<OauthClientDetails>> getListByUser(@RequestHeader("x-auth-uid") Long userId , @RequestBody GetListByUserRequest getListByUserRequest) {
        getListByUserRequest.setUserId(userId);
        return ApiResponse.success(oauthClientDetailsService.getListByUser(getListByUserRequest));
    }

    @PostMapping("/client/add")
    public ApiResponse<OauthClientDetails> auth(@RequestHeader("x-auth-uid") Long userId, @RequestBody OauthClientDetailAddRequest oauthClientDetailAddRequest) {
        oauthClientDetailAddRequest.setUserId(userId);
        return ApiResponse.success(oauthClientDetailsService.add(oauthClientDetailAddRequest));
    }

    @PostMapping("/client/getById")
    public ApiResponse<OauthClientDetails> getById(@RequestParam("id") String id) {
        return ApiResponse.success(oauthClientDetailsService.getById(id));
    }

    @PostMapping("/client/updateById")
    public ApiResponse<Integer> updateById(@RequestBody OauthClientDetails oauthClientDetails) {
        return ApiResponse.success(oauthClientDetailsService.updateById(oauthClientDetails));
    }
}
